import DBConnection from "../../service/unit/mysql/DBConnection";
import File from "../../service/file/File";


const TABLE_NAME = 'article';

/**
 * 获取文章名,作者名与文字路径
 * @param conn DBConnection类实例
 * @param articleId {number}
 * @return article表中符合条件的结果数组
 * */
function queryArticle(conn:DBConnection, articleId:number) {
    const _knex = conn.knex;
    return _knex.where({id:articleId}).select().table(TABLE_NAME);
}

export interface IResult {
    content: string|Buffer
    author: string|Buffer
    title: string|Buffer
}

/**
 * @return {string} 返回读取文件的内容
 * */
export async function readArticleFile(conn:DBConnection, articleId:number):Promise<IResult>{
    const queryResult = await queryArticle(conn, articleId);
    const {path, title, author} = queryResult[0];
    const _path = conn.strDecode(path);
    const _title = conn.strDecode(title);
    const _author = conn.strDecode(author);


    return {
        content: await File.readFile(_path, _title),
        author: _author,
        title: _title
    };
}



